#ifndef USART_DRIVER_H
#define USART_DRIVER_H
//#ifdef SUPPORT_BLUETOOTH
/*---------------------------------Define Fcpu-------------------------------*/
#define Fcpu ((unsigned long) 8000000)   

/******************************************************************************
BIT DEFINITIONS
******************************************************************************/
/**************************Status Register bits*******************************/
#define USART_TXE		  0x80	//transmit data register empty
//This bit is set by hardware when the content of the TDR register has been
//transferred into the shift register. An interrupt is generated if the TIEN bit =1 in the
//USART_CR2 register. It is cleared by a write to the USART_DR register.
//0: Data is not transferred to the shift register
///1: Data is transferred to the shift register
#define USART_TC            0x40
///This bit is set by hardware when transmission of a frame containing Data is
///complete. An interrupt is generated if TCIEN=1 in the USART_CR2 register. It is
///cleared by a software sequence (a read to the USART_SR register followed by a
///write to the USART_DR register).
///0: Transmission is not complete
///1: Transmission is complete
#define USART_RDRF        0x20///Read data Register Not Empty.
#define USART_IDLE        0x10///IDLE line detected.
#define USART_ORE         0x08///OverRun error.
#define USART_NE          0x04///Noise Flag.
#define USART_FE          0x02///Framing Error.
#define USART_PE          0x01///Parity Error.

/*********************Control Register bits**********************************/

/************************USART_CR1**************************************/
#define USART_RX_9TH	0x80///This bit is used to store the 9th bit of the received word when M=1
#define USART_TX_9TH	0x40///This bit is used to store the 9th bit of the transmitted word when M=1
#define USART_SARTD		0x20///USART Disable (for low power consumption).
#define USART_M		    0x10///word length.
///This bit determines the word length. It is set or cleared by software.
///0: 1 Start bit, 8 Data bits, n Stop bit (n depending on STOP[1:0] bits in the USART_CR3 register)
///1: 1 Start bit, 9 Data bits, 1 Stop bit
///Note: The M bit must not be modified during a data transfer (both transmission and reception)

#define USART_WAKE	    0x08///Wakeup method.
#define USART_PCEN	    0x04///Parity Control Enable.
#define USART_PS		0x02///Parity Selection.
#define USART_PIEN		0x01///Parity Interrupt Enable.
/************************USART_CR2**************************************/

#define USART_TDRE    	0x80///TIEN transmitter interrupt enable 
#define USART_TCIEN	    0x40///Transmission Complete Interrupt Enable.
#define USART_RIEN		0x20///Receiver Interrupt Enable.
#define USART_ILIEN	    0x10///IDLE Line Interrupt Enable.
#define USART_TEN		0x08///Transmitter Enable.It is set and cleared by software.
#define USART_REN		0x04///Receiver Enable. It is set and cleared by software.
#define USART_RWU		0x02///Receiver wakeup.
///This bit determines if the USART2 is in mute mode or not. It is set and cleared by
///software and can be cleared by hardware when a wakeup sequence is recognized.

#define USART_SBK		0x01///Send Break.
/************************USART_CR3**************************************/

/************************USART_CR4**************************************/

/************************USART_CR5**************************************/

/*********************Boud Rate Register*************************************/
#define USART_DIV_BRR1	0x34		//9600 0341h  8000000/9600
#define USART_DIV_BRR2	0x01

#define USART_ErrValue  0x0f        /*Error bits in SR register*/

typedef enum{///USART_CR1
	USART_DEFAULT_PARAM1      = (unsigned char) 0x00,    
	USART_WORDLENGTH_9        = (unsigned char) 0x10, //SCL_M
	USART_ODPARITY_SELECT     = (unsigned char) 0x06,
	USART_EVPARITY_SELECT     = (unsigned char) 0x04,
	USART_WAKEUP_ADDR         = (unsigned char) 0x08///Wakeup method.
}USART_Type_Param1;

typedef enum {                 /*Parameters for Control Register2*/
	USART_MUTE_ENABLE         = (unsigned char) 0x02, 
	USART_BREAK_ENABLE        = (unsigned char) 0x01,
	USART_DEFAULT_PARAM2      = (unsigned char) 0x00
}USART_Type_Param2;                  
typedef enum {                 /*Parameters for enabling interrupts*////USART_SR
	USART_IDLE_LINE               = (unsigned char) 0x10,
	USART_RECEIVE_OVERRUN         = (unsigned char)0x20,
	USART_TRANSMIT_REGISTER_READY = (unsigned char) 0x80,
	USART_FRAME_TRANSMITTED       = (unsigned char) 0x40,
	USART_PARITY_ERROR            = (unsigned char) 0x01
}USART_IT_Type;

typedef enum {                /*Parameters for enabling mode*/ //USART_CR2
	USART_TX_ENABLE        = (unsigned char) 0x08,
	USART_RX_ENABLE        = (unsigned char) 0x04
 }USART_Mode_Type;

#define USART_Mode2(Usart_Mode_Param)  ((USART_CR2) |= (Usart_Mode_Param));

#define USART_TX_EN()	{(USART_CR2) |=USART_TX_ENABLE;  (USART_CR1) &=0xdf; }
#define USART_TX_DIS()	{(USART_CR2) &=~USART_RX_ENABLE;  (USART_CR1) |=0x20; }
/*************************** For Selecting the Baudrate **********************/
#define USART_Select_Baudrate(Baudrate_DivM,Baudrate_DivF) { USART_BRR2 =Baudrate_DivF;USART_BRR1= Baudrate_DivM;}

#define USART_DIS()		{(USART_CR2) &=~USART_TX_ENABLE;(USART_CR2) &=~USART_RX_ENABLE;  (USART_CR1) |=0x20; }



/***************************Function declarations*****************************/

extern void USART_Init ( USART_Type_Param1  Init_Value1, USART_Type_Param2  Init_Value2); /*Initialises the SCI*/
extern void USART_IT_Enable(USART_IT_Type USART_IT_Param);/*Disables the desired the interrupt */
extern void USART_PutByte (unsigned char Tx_DATA);    /*Transmits a single data byte*/
extern bool USART_IsTransmitCompleted( void);    /*Checks if transmission is completed*/
extern void USART_PutBuffer (const unsigned char *PtrtoBuffer, signed char Length);
                                                    /*Transmits a user buffer*/
extern void USART_PutString(const unsigned char *PtrtoString); /*Transmits a string*/
extern void  ISR_USART (void);
/*Carries out communication in Interrupt Driven mode*/
extern void BT_UART_Rx_Data(int8u bRxData);

#endif

